<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
    <meta name="description" content="">

    <link rel="stylesheet" href="/lib/weui.min.css">
    <link rel="stylesheet" href="/css/jquery-weui.css">

    <link rel="stylesheet" href="/plugins/fontaw/css/font-awesome.css">
    <link rel="stylesheet" href="/plugins/kim/style.css">

    <style>

    </style>
</head>
<body>
<a href="javascript:pay()"> 支付 </a>


<script src="/lib/jquery-2.1.4.js"></script>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js" charset="utf-8"></script>

<script>

    $(document).ready(function(){
        //initConfig();
    });

   /* function initConfig(){
        $.ajax({
            url:"/wechat/jsconfig",
            dataType:"json",
            data:{"vurl":location.href},
            success:function(da){
                wx.config({
                    debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                    appId: da.data.appId, // 必填，公众号的唯一标识
                    timestamp: da.data.timestamp, // 必填，生成签名的时间戳
                    nonceStr: da.data.nonceStr, // 必填，生成签名的随机串
                    signature: da.data.signature,// 必填，签名
                    jsApiList: ["chooseImage","previewImage","uploadImage","onMenuShareAppMessage","chooseWXPay"] // 必填，需要使用的JS接口列表
                });
            }
        });
    }*/
    var orderOk = false;
    var payParam;
    function onBridgeReady(){
        if(orderOk) {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                    "appId": payParam.appId,     //公众号名称，由商户传入
                    "timeStamp": payParam.timeStamp,         //时间戳，自1970年以来的秒数
                    "nonceStr": payParam.nonceStr, //随机串
                    "package": payParam.package,
                    "signType": payParam.signType,         //微信签名方式：
                    "paySign": payParam.paySign //微信签名
                },
                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        alert("支付成功");
                    } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                        alert("您已取消支付");
                    } else if (res.err_msg == "get_brand_wcpay_request:fail") {
                        alert("支付失败：" + JSON.stringify(res));
                    }
                }
            );
        }

    }

    function pay(){
        //先去下单
        $.ajax({
            url:"/wcpay/wcorder",
            dataType:"json",
            async:false,
            success:function(da){
                if(da.msg == "ok"){
                    orderOk = true;
                    payParam = da.data;
                    alert(JSON.stringify(payParam));
                }
            }
        });

        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
        }else{
            onBridgeReady();
        }
    }


</script>
</body>
</html>
